package com.hc.fdkforwarders.dao;

import com.hc.fdkforwarders.common.page.PageContentContainer;
import com.hc.fdkforwarders.common.utils.StringUtils;
import com.hc.fdkforwarders.vo.GoodsFindQueryVO;
import com.hc.fdkforwarders.vo.GoodsFindVO;
import com.hc.fdkforwarders.vo.SkuVO;
import org.jooq.Record8;
import org.jooq.SelectConditionStep;
import org.jooq.SelectOnConditionStep;
import org.springframework.stereotype.Repository;


import static com.hc.fdkforwarders.db.tables.PlatformSpu.PLATFORM_SPU;
import static com.hc.fdkforwarders.db.tables.PlatformSku.PLATFORM_SKU;

@Repository
public class GoodsFindDao extends BaseDao {
    public PageContentContainer<SkuVO> getGoodsList(GoodsFindQueryVO goodsFindQuery) {
//        SelectConditionStep<?> query =
        SelectConditionStep<?> query = db.select(
                PLATFORM_SKU.ID.as("skuid"), PLATFORM_SKU.SKU_NAME,
                PLATFORM_SKU.REF_FETCH_PRICE, PLATFORM_SKU.REAL_FETCH_PRICE,
                PLATFORM_SKU.BIZ_FULL_NAME, PLATFORM_SPU.MAIN_IMG_URL, PLATFORM_SPU.TOTAL_FETCH.as("spuFetchNum"),
                PLATFORM_SPU.SPU_GOODS_NO, PLATFORM_SPU.ID.as("spuid"), PLATFORM_SPU.TOTAL_RETURN.as("spuReturnNum"))
                .from(PLATFORM_SKU)
                .innerJoin(PLATFORM_SPU)
                .on(PLATFORM_SKU.SPU_ID.eq(PLATFORM_SPU.ID))
                .where(PLATFORM_SPU.CITY_ID.eq(goodsFindQuery.getCityId()));
        if(!StringUtils.isEmpty(goodsFindQuery.getBizFullName())) {
           query = query.and(PLATFORM_SKU.BIZ_FULL_NAME.eq(goodsFindQuery.getBizFullName()));
           query = query.and(PLATFORM_SPU.FLOOR_CODE.eq(goodsFindQuery.getFloorId()))
                   .and(PLATFORM_SPU.SPU_GOODS_NO.like("%" + goodsFindQuery.getSpuGoodsNo() + "%"))
                   .and(PLATFORM_SPU.MARKET_ID.eq(goodsFindQuery.getMarketId()));
        }
        query.orderBy(PLATFORM_SKU.BIZ_FULL_NAME.asc(), PLATFORM_SPU.SPU_GOODS_NO.asc());


        return getPageResult(query, goodsFindQuery, SkuVO.class);
    }
}
